home *** CD-ROM | disk | FTP | other *** search
- Path: news.nask.org.pl!usenet
- From: piotrpar@blue.maloka.waw.pl (Piotr Parlewicz)
- Newsgroups: comp.lang.c++
- Subject: Re: overloading ofstream constructor, help needed
- Date: Tue, 09 Apr 1996 18:45:44 GMT
- Organization: Research and Academic Computer Network
- Message-ID: <4keb0k$d5e@bilbo.nask.org.pl>
- References: <4k48qe$o9v@hobbes.cc.uga.edu>
- NNTP-Posting-Host: s114.maloka.waw.pl
- X-Newsreader: Forte Free Agent 1.0.82
-
- greear@pollux.cs.uga.edu (Ben Greear) wrote:
-
-
- >I have a string class: String
-
- >I would like to be able to get an ofstream type file stream like this:
-
- >String buf("./my_log_file");
-
- >ofstream my_ofstream(buf);
-
-
- >I think this would entail overloading ofstream somehow, and undoubtedly
- >some other c++ wizardry :)
-
-
- >if anyone has an example, or advice, I would love to look at it!!
-
- >Ben Greear | "The meek shall inherit the Earth,
- >greear@pollux.cs.uga.edu | the rest of us are going to the Stars!!
- >Junior at UGA --Bumper Sticker
-
-
- Hi,
- all you need to do is define a type cast operator for your class
- String. You will be casting to the type that ofstream requires, this
- is 'const char *'.
-
- So to your class String you add the following:
- operator const char*() const {
- // your code that returns a pointer to a c type string contained in
- // the String object
- }
-
- Piotr Parlewicz
- piotrpar@blue.maloka.waw.pl
-
-
-
-
-
-
-
-